home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
5 Star Games: DOS Edition 2
/
5 Star Games - DOS Edition (1995)(Ready to Run).iso
/
dbc
/
db_date.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-28
|
13KB
|
516 lines
/****************************************************************************/
/* DATABOSS MODULE: DB_DATE.C */
/****************************************************************************/
#include "db_lsc.h"
#ifdef __TURBOC__
#include <conio.h>
#else
#include <graph.h>
#endif
#include <dos.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "db_types.h"
#include "db_conio.h"
#include "db_key.h"
#include "db_math.h"
#include "db_sets.h"
#include "db_str.h"
#include "db_date.h"
#include "db_file.h"
#include "db_funcs.h"
/**************************** GLOBAL CONSTANTS ****************************/
const byte Limit[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31};
/**************************** GLOBAL VARIABLES ****************************/
set _NUMERIC;
_datetyp dateformat;
uchar defaultyr[3];
byte daypos;
byte mthpos;
byte yrpos;
/*************************** INTERNAL VARIABLES ***************************/
static bool initialized = False;
/***************************** IMPLEMENTATION *****************************/
bool leapyr(int y)
{
return ((bool) (((y % 4 == 0) && (y % 100 > 0)) || (y % 400 == 0)));
}
strptr slash(_timestr sout, _timestr datein)
{
uchar temp1[3],temp2[3],temp3[5];
if (yrpos == 5) {
strcopy(temp1,datein,0,2);
strcopy(temp2,datein,2,2);
strcopy(temp3,datein,4,4);
strconcat(sout,temp1,"/",temp2,"/",temp3,NULL);
}
else
strconcat(sout,strcopy(temp1,datein,0,4),"/",
strcopy(temp2,datein,4,2),"/",
strcopy(temp3,datein,6,4),NULL);
return (sout);
}
strptr slasht(_timestr sout, _timestr timein)
{
uchar dpt[2];
uchar temp1[3],temp2[3],temp3[5];
strcpy(dpt, ".");
dpt[0] = *_dcpt;
strconcat(sout,strcopy(temp1,timein,0,2),timesep,
strcopy(temp2,timein,2,2),timesep,
strcopy(temp3,timein,4,2),NULL);
if (strlen(timein) > 6) strconcat(sout,sout,dpt,strcopy(temp1,timein,6,2),NULL);
return (sout);
}
bool chkdate(_datestr date)
{
int i;
strptr sourceDate,destDate;
_datestr tempDate;
uchar temp[5];
bool date_ok,leapyear;
int year,month,day;
/* SN 3.5
for (s=date, d=tdate, i=1; (*s != '\0') && (i <= 8); s++, i++) *(d++) = (uchar) ((*s == ' ') ? '0' : *s);
*/
sourceDate = date; /* SN 3.5 */
destDate = tempDate; /* SN 3.5 */
strcpy(destDate, sourceDate); /* SN 3.5 */
if (*(destDate + 6) == ' ') *(destDate + 6) = '\0'; /* SN 3.5 */
else { /* SN 3.5 */
if (*(destDate + 7) == ' ') *(destDate + 7) = '\0'; /* SN 3.5 */
} /* SN 3.5 */
if (strlen(destDate) <= 6) strinsert(defaultyr, destDate, yrpos-1); /* SN 3.5 */
date_ok = False;
day = ival(strcopy(temp,destDate,daypos-1,2));
month = ival(strcopy(temp,destDate,mthpos-1,2));
year = ival(strcopy(temp,destDate,yrpos-1,4));
leapyear = leapyr(year);
if (((month > 0) && (month < 13)) && ((day > 0) && (day < 32))) date_ok = True; /* SN 3.5 */
if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day > 30)) date_ok = False; /* SN 3.5 */
if ((date_ok && (month == 2)) && ((leapyear && (day > 29)) || (!leapyear && (day > 28)))) date_ok = False; /* SN 3.5 */
return (date_ok);
}
long ctod(_datestr date)
{
int i;
strptr sourceDate,destDate;
_datestr tempDate;
uchar temp[5];
int year,month,day;
long julian;
/* SN 3.5
for (s=date, d=tdate, i=1; (*s != '\0') && (i <= 8); s++, i++) *(d++) = (uchar) ((*s == ' ') ? '0' : *s);
*d = '\0';
*/
sourceDate = date; /* SN 3.5 */
destDate = tempDate; /* SN 3.5 */
strcpy(destDate, sourceDate); /* SN 3.5 */
if (*(destDate + 6) == ' ') *(destDate + 6) = '\0'; /* SN 3.5 */
else { /* SN 3.5 */
if (*(destDate + 7) == ' ') *(destDate + 7) = '\0'; /* SN 3.5 */
} /* SN 3.5 */
if (strlen(destDate) <= 6) strinsert(defaultyr, destDate, yrpos-1); /* SN 3.5 */
day = ival(strcopy(temp,destDate,daypos - 1,2));
if (day == 0) day = 1;
month = ival(strcopy(temp,destDate,mthpos-1,2));
year = ival(strcopy(temp,destDate,yrpos-1,4));
julian = (long) fint(fint(30.57 * month) + fint(365.25 * year - 395.25) + day);
if (month > 2) {
julian--;
if (!leapyr(year)) julian--;
}
return (julian);
}
void dton(long jul, int *day, int *month, int *year)
{
double temp,julian;
int leapday;
julian = (double) jul;
*year = (int) (fint(julian/365.26) + 1);
temp = fint(julian+fint(395.25-(365.25*(*year))));
leapday = 1;
if (!leapyr(*year)) leapday = 2;
if (temp > (double) (91 - leapday)) temp += (double) leapday;
*month = (int) fint(temp/30.57);
*day = (int) fint(temp - fint(30.57 * (*month)));
if (*month > 12) {
*month = 1;
(*year)++;
}
}
strptr dtoc(_datestr date, long julian)
{
uchar sday[7],smonth[7],syear[7];
int day,month,year;
strptr dptr;
dton(julian,&day,&month,&year);
istr(sday,day, 2);
istr(smonth,month, 2);
istr(syear,year, 4);
memmove(&date[daypos-1],sday,2);
memmove(&date[mthpos-1],smonth,2);
memmove(&date[yrpos-1],syear,4);
date[8] = '\0';
for (dptr = date; *dptr != '\0'; dptr++) if (*dptr == ' ') *dptr = '0';
return (date);
}
int dow(long jul)
{
double julian;
julian = (double) (jul-1);
return ((int) fint(julian - (fint(julian / 7) * 7) + 1));
}
strptr cdow(_str9 sout, long julian)
{
int d;
d = dow(julian);
return (strcpy(sout, DayTab[((d >= 1) && (d <= 7)) ? d : 0]));
}
strptr cmonth(_str9 sout, long julian)
{
int day,mon,year;
dton(julian,&day,&mon,&year);
return (strcpy(sout,MthTab[((mon >= 0) && (mon <= 12)) ? mon : 0]));
}
int getday(long julian)
{
int day,month,year;
dton(julian,&day,&month,&year);
return (day);
}
int getmonth(long julian)
{
int day,month,year;
dton(julian,&day,&month,&year);
return (month);
}
int getyear(long julian)
{
int day,month,year;
dton(julian,&day,&month,&year);
return (year);
}
strptr idxdate(_datestr sout, _datestr dat)
{
double r;
r = (double) ctod(dat);
fstr(sout, r, 6, 0);
pad(sout, strip(sout, sout), 8, Right);
return (sout);
}
strptr decdate(_datestr sout, _datestr dat)
{
double r;
r = 99999999.0 - ctod(dat);
fstr(sout, r, 8, 0);
return (sout);
}
void datediff(long jul1, long jul2, int *d, int *m, int *y)
{
int d1,d2,m1,m2,y1,y2,sav_m2,back_month;
long swapthem;
if (jul1 > jul2) {
swapthem = jul1;
jul1 = jul2;
jul2 = swapthem;
}
dton(jul1,&d1,&m1,&y1);
dton(jul2,&d2,&m2,&y2);
sav_m2 = m2;
back_month = m2-1;
if (back_month == 0) back_month = 12;
*y = y2 - y1;
if ((m2 < m1) || ((m2 == m1) && (d2 < d1))) {
(*y)--;
if (m2 < m1) m2 += (d2 < d1) ? 11 : 12;
if (d2 < d1) {
if (sav_m2 == m1) m2 += 11;
d2 = d2 + Limit[back_month-1];
if (back_month == 2) d2--;
}
}
*m = m2 - m1;
if ((sav_m2 > m1) && (d2 < d1)) {
(*m)--;
d2 += Limit[back_month-1];
if (back_month == 2) d2--;
}
*d = d2 - d1;
}
int leapdays(long jul1, long jul2)
{
int i,leaps,d1,d2,m1,m2,y1,y2;
long swapthem;
leaps = 0;
if (jul1 > jul2) {
swapthem = jul1;
jul1 = jul2;
jul2 = swapthem;
}
dton(jul1,&d1,&m1,&y1);
dton(jul2,&d2,&m2,&y2);
for (i = y1; i <= y2; i++) if (leapyr(i)) leaps++;
if (leapyr(y1) && (m1 > 2)) leaps--;
if (leapyr(y2) && (m2 == 1) || ((m2 == 2) && (d2 <= 29))) leaps--;
return (leaps);
}
strptr datum(_str30 sout, _datestr sdate)
{
int i;
long d;
uchar s1[40],s2[40];
d = ctod(sdate);
cdow(s1,d);
i = strlen(s1);
while (i && (s1[i-1] == ' ')) i--;
s1[i] = '\0';
strconcat(s1,s1,", ",cmonth(s2,d),NULL);
i = strlen(s1);
while (i && (s1[i-1] == ' ')) i--;
s1[i] = '\0';
istr(s2, getday(d), 2);
strconcat(s1,s1," ",s2,", ",NULL);
istr(s2,getyear(d), 4);
strconcat(sout,s1,s2,".",NULL);
i = strlen(sout);
while (i < 30) sout[i++] = ' ';
sout[i] = '\0';
return (sout);
}
strptr days(_str6 sout, long d1, long d2)
{
int d,m,y;
datediff(d1,d2,&d,&m,&y);
istr(sout, d, 2);
return (sout);
}
strptr months(_str6 sout, long d1, long d2)
{
int d,m,y;
datediff(d1,d2,&d,&m,&y);
istr(sout,m, 2);
return (sout);
}
strptr years(_str6 sout, long d1, long d2)
{
int d,m,y;
datediff(d1,d2,&d,&m,&y);
istr(sout,y, 4);
return (sout);
}
strptr sysdate(_datestr sout)
{
union REGS regs;
uchar d[7],m[7],y[7];
strptr sptr;
regs.x.ax = 0x2A00;
intdos(®s,®s);
istr(d,regs.h.dl, 2);
istr(m,regs.h.dh, 2);
istr(y,regs.x.cx, 4);
memmove(&sout[daypos-1],d,2);
memmove(&sout[mthpos-1],m,2);
memmove(&sout[yrpos-1],y,4);
sout[8] = '\0';
for (sptr = sout; *sptr ; sptr++) if (*sptr == ' ') *sptr = '0'; /* SN 3.5 */
return (sout);
}
strptr systime(_timestr sout)
{
union REGS regs;
uchar h[7],m[7],s[7],d[7];
regs.x.ax = 0x2C00;
intdos(®s,®s);
istr(h,regs.h.ch,2);
istr(m,regs.h.cl,2);
istr(s,regs.h.dh,2);
istr(d,regs.h.dl,2);
if (*h == ' ') *h = '0';
if (*m == ' ') *m = '0';
if (*s == ' ') *s = '0';
if (*d == ' ') *d = '0';
return (strconcat(sout,h,m,s,d,NULL));
}
double realtime(void)
{
union REGS regs;
regs.x.ax = 0x2C00;
intdos(®s,®s);
return ((3600.0 * regs.h.ch) +
(60.0 * regs.h.cl) +
(1.0 * regs.h.dh) +
((1.0 * regs.h.dl)/100.0));
}
strptr get_time(_timestr sout, _timemode mode)
{
_timestr outtime,temp;
uchar hour_str[7];
int hour;
slasht(outtime,strcopy(outtime,systime(outtime),0,6));
hour = ival(strcopy(temp,outtime,0,2));
istr(hour_str,hour, 2);
strconcat(outtime,hour_str,strcopy(temp,outtime,2,6),NULL);
if (mode == _AmPm) strcopy(outtime,outtime,0,5);
if (mode != _24Hr) {
if (hour < 12)
strconcat(outtime,outtime,_AM_,NULL);
else {
strconcat(outtime,outtime,_PM_,NULL);
if (hour > 12) {
strdelete(outtime,0,2);
istr(hour_str,hour-12, 2);
strconcat(outtime,hour_str,outtime,NULL);
}
}
}
return ((mode==_Full) ? strcpy(sout,outtime) : strcopy(sout,outtime,0,8));
}
strptr strtime(_timestr sout, byte mode, _timestr timein)
{
int tr;
uchar dpt[2];
uchar hrss[3], mins[3], secs[3], huns[3], ampms[4];
strcpy(dpt, ".");
dpt[0] = *_dcpt;
strcopy(hrss,timein,0,2);
strcopy(mins,timein,2,2);
strcopy(secs,timein,4,2);
strcopy(huns,timein,6,2);
sout[0] = '\0';
tr = ival(hrss);
if (tr >= 12) strcpy(ampms,_PM_); else strcpy(ampms,_AM_);
if (((mode & _12hr)) && (tr > 12)) tr = (tr % 13) + 1; /* SN 3.5 */
istr(hrss,tr, 2);
if ((mode & _hours)) strcpy(sout,hrss); /* SN 3.5 */
if ((mode & _mins)) strconcat(sout,sout,timesep,mins,NULL); /* SN 3.5 */
if ((mode & _secs)) strconcat(sout,sout,timesep,secs,NULL); /* SN 3.5 */
if ((mode & _huns)) strconcat(sout,sout,dpt,huns,NULL); /* SN 3.5 */
if ((mode & _am_pm)) strcat(sout,ampms); /* SN 3.5 */
return(sout);
}
void show_time(int x, int y, int attr, _timemode mode)
{
_timestr outtime;
byte tb,saveattr;
getscreeninfo(&tb,&tb,&tb,&saveattr);
textattr(attr);
while (!kpressed()) {
get_time(outtime,mode);
gotoxy(x,y);
cwrite(outtime);
}
textattr(saveattr);
}
bool long_date(string field)
{
return ((bool) (chkdate(field) &&
(strchr(field,' ') == NULL) &&
(strlen(field) == 8)));
}
/********************** UNIT INITIALIZATION/EXIT CODE *********************/
void db_date_init(void)
{
_datestr td;
string numericCharacters;
if (!initialized) {
initialized = True;
db_key_init();
dateformat = cdi.date_format;
switch (dateformat) {
case USA: daypos = 3; mthpos = 1; yrpos = 5; break;
case EUR: daypos = 1; mthpos = 3; yrpos = 5; break;
case JAP: daypos = 7; mthpos = 5; yrpos = 1; break;
}
strcopy(defaultyr,sysdate(td),yrpos-1,2);
*_dcpt = *cdi.dec_sep;
*_thou = *cdi.thou_sep;
*datesep = *cdi.date_sep;
*timesep = *cdi.time_sep;
*(_dcpt + 1) = '\0';
*(_thou + 1) = '\0';
*(datesep + 1) = '\0';
*(timesep + 1) = '\0';
strcpy(numericCharacters, "0123456789- ");
strcat(numericCharacters, _dcpt);
set_strmake(_NUMERIC,numericCharacters);
}
}
/***************************** END OF DB_DATE.C ****************************/